#include "../skyAdapter.h"
#ifdef WIN32
#include "../../adapter/skyPainter.h"

namespace Sky
{

	TVoid Painter::drawLine( TPoint& start,TPoint& end )
	{
		D3DXVECTOR2 vStart(start.x,start.y);
		D3DXVECTOR2 vEnd(end.x,end.y);
		m_Sprite.end();
		TResult hr = m_Pen.drawLine(&vStart,&vEnd);
		m_Sprite.begin();
	}

	TVoid Painter::drawPict( TRect& dstRect,Pict* iPict,TRect* srcRect )
	{
		TRect rSrc;
		TRect* pSrc = vNull;
		Pict* pict = (Pict*)iPict;
		TRect rect = TRect(0,0,iPict->getSize().w,iPict->getSize().h);
		xAssert(iPict);
		if(vNull == srcRect)
		{
			srcRect = &rect;
		}
		if(srcRect)
		{
			rSrc.x = srcRect->x;
			rSrc.y = srcRect->y;
			rSrc.w = srcRect->w;
			rSrc.h = srcRect->h;
			pSrc = &rSrc;
			if((dstRect.h != srcRect->h) ||
				(dstRect.w != srcRect->w))
			{
				m_Sprite.draw(pict->getPict(),pSrc,&xVector5(dstRect.x,dstRect.y ,dstRect.w,dstRect.h,0),vNull,D3DCOLOR_RGBA(0xff,0xff,0xff,0xff));
				return;
			}
		}
		else
		{

			if((dstRect.h != iPict->getSize().h) ||
				(dstRect.w != iPict->getSize().w))
			{
				rSrc.x = 0;
				rSrc.y = 0;
				rSrc.w = iPict->getSize().w;
				rSrc.h = iPict->getSize().h;
				pSrc = &rSrc;
				m_Sprite.draw(pict->getPict(),pSrc,&xVector5(dstRect.x,dstRect.y ,dstRect.w,dstRect.h,0),vNull,D3DCOLOR_RGBA(0xff,0xff,0xff,0xff));
				return;
			}
		}
		m_Sprite.draw(pict->getPict(),pSrc,&xVector3(dstRect.x,dstRect.y ,1.0),vNull,D3DCOLOR_RGBA(0xff,0xff,0xff,0xff));
	}

	TVoid Painter::drawText(TRect& rect,const String* text,TColor color,Layout::Align align) 
	{
		m_Sprite.draw(const_cast<String*>(text)->toCharString(),&rect,-1,align,D3DCOLOR_RGBA(color.r,color.g,color.b,color.a));
	}

	TVoid Painter::setPenColor( TColor color )
	{
		D3DXCOLOR clr = D3DCOLOR_RGBA(color.r,color.g,color.b,color.a);
		m_Pen.setColor(clr);
	}

	Painter::Painter()
	{
		m_Sprite.setCenter(0,0,0);
		m_Pen.setWidth(1.0f);
		m_Brush.setWidth(50.0f);
		m_Pen.setPattern(1);
		m_Brush.setPattern(1);
	}

	TBool Painter::begin()
	{
		D3DVIEWPORT9 viewPort;
		viewPort.X = mClipRect.x;
		viewPort.Y = mClipRect.y;
		viewPort.Width = mClipRect.w;
		viewPort.Height = mClipRect.h;
		viewPort.MinZ = 0;
		viewPort.MaxZ = 1.0f;
		//m_RT.setViewPort(&viewPort);
		return XE_OK == m_Sprite.begin() ? vTrue : vFalse;
	}

	TVoid Painter::end()
	{
		m_Sprite.end();
		//m_RT.setViewPort(vNull);
	}

	TVoid Painter::fillRect( TRect& rect )
	{
		if(rect.h == 0)
			return;
		m_Brush.setWidth(rect.h);
		D3DXVECTOR2 vStart(rect.x,rect.y + rect.h/2);
		D3DXVECTOR2 vEnd(rect.x + rect.w,rect.y + rect.h/2);
		m_Sprite.end();
		TResult hr = m_Brush.drawLine(&vStart,&vEnd);
		m_Sprite.begin();
	}

	TVoid Painter::setBrushColor( TColor color )
	{
		D3DXCOLOR clr = D3DCOLOR_RGBA(color.r,color.g,color.b,color.a);
		m_Brush.setColor(clr);
	}

	TBool Painter::attachPict( Pict* iPict )
	{
		if(XE_OK == m_Sprite.attachPict(iPict->getPict()))
		{
			mpPict = iPict;
			return vTrue;
		}
		else
			return vFalse;
	}

	Pict* Painter::detachPict()
	{
		PictPtr prev = mpPict;
		mpPict = vNull;
		return prev;
	}

	TBool Painter::attachFont( Font* iFont )
	{

		if(XE_OK == m_Sprite.attachFont(&iFont->getFont()))
		{
			mpFont = iFont;
			return vTrue;
		}
		return 
			vFalse;
	}

	Font* Painter::detachFont()
	{
		FontPtr prev = mpFont;
		m_Sprite.detachFont();
		mpFont = vNull;
		return prev;
	}

	//TRect* Painter::getClipArea()
	//{
	//	return &mClipRect;
	//}

	//TVoid Painter::setClipArea( TUint x,TUint y,TUint w,TUint h )
	//{

	//	return mClipRect.set(x,y,w,h);	
	//}

	/*TVoid Painter::setClipArea( TRect* rect )
	{
		if(rect == vNull)
		{
			TSize size = mpPict->getSize();
			setClipArea(0,0,size.w,size.h);
		}
		else
			mClipRect.set(rect->x,rect->y,rect->w,rect->h);
		return;
	}

	TRect* Painter::getInvalidArea()
	{
		return &mDirtyRect;
	}

	TVoid Painter::setInvalidArea( TUint x,TUint y,TUint w,TUint h )
	{
		return mDirtyRect.set(x,y,w,h);
	}

	TVoid Painter::setInvalidArea( TRect* rect )
	{
		if(rect == vNull)
		{
			TSize size = mPict->getSize();
			setInvalidArea(0,0,size.w,size.h);
		}
		else
			m_DirtyRect.set(rect->x,rect->y,rect->w,rect->h);
		return;
	}*/

	TBool Painter::fillWith( Pict* srcPict,TRect* srcRect,TRect* dstRect )
	{
		return vTrue;
	}

	TBool Painter::mirrorWith( Pict* srcPict,Pict* dst,TUint MirrorType_)
	{
		return vTrue;
	}

	TVoid Painter::drawRect( TRect& rect )
	{
		drawLine( TPoint(rect.x,rect.y),TPoint(rect.x,rect.y + rect.h));
		drawLine( TPoint(rect.x,rect.y),TPoint(rect.x + rect.w,rect.y));
		drawLine( TPoint(rect.x,rect.y + rect.h),TPoint(rect.x + rect.w, rect.y + rect.h));
		drawLine( TPoint(rect.x + rect.w,rect.y),TPoint(rect.x + rect.w, rect.y + rect.h));
	}

	TSize Painter::calTextSize( String* str ,Font* font)
	{
		TRect r;
		r.x = 0;
		r.y = 0;
		FontPtr oldFont;
		if(font != vNull)
		{
			oldFont = getFont();
			attachFont(font);
		}
		TSize ret = m_Sprite.calTextSize(&mpFont->getFont(),str->toCharString(),&r);
		if(font != vNull)
		{
			attachFont(oldFont);
		}
		return TSize(ret.w,ret.h);
	}

	Sky::NType::TBool Painter::attachCanvas( Canvas* canvas )
	{
		mCanvas = canvas;
		mCanvas->attachPicture();
		return vTrue;
	}

	Canvas* Painter::detachCanvas()
	{
		Canvas* p = mCanvas;
		p->detachPicture();
		mCanvas = vNull;
		return p;
	}
}
#endif
